<%@ include file="database_ini.jsp"%>


<%
	String dropid = request.getParameter("DROPID");
	request.setAttribute("DROPID",dropid);
	response.sendRedirect("index.jsp?DROPID=" +dropid);
	/*********debug*/
	System.out.println("DROPID: " + dropid);
	/**********/
	
	String Seq[] = request.getParameterValues("Seq");
	if (Seq != null) {
		for (int i = 0; i < Seq.length; i++) {
			out.println(Seq[i]);
			System.out.println("Seq: " + Seq[i]);
		}
	}
	
	String CountQty_str[] = request.getParameterValues("CountQty");
	int CountQty_int[] = new int[CountQty_str.length];
	if (CountQty_str != null) {
		for (int i = 0; i < CountQty_str.length; i++) {
			
			if(CountQty_str[i]!=""){
			
				CountQty_int[i] = Integer.parseInt(CountQty_str[i]);
			}
			else {
				CountQty_int[i] = -1;
			}
			/*********debug*/
			System.out.println("CountQty: " + CountQty_str[i]);
			System.out.println("CountQty_str.length: "
					+ CountQty_str.length);
			System.out.println("CountQty_int: " + CountQty_int.length);
			/**********/
		}
	}
	
	System.out.println("CountQty_int: " + CountQty_int);
	
	for (int a = 0; a < CountQty_int.length; a++) {

		if(CountQty_int[a] != -1){

			int CountQty_int_re = CountQty_int[a];
			int CountQty_int_cur = 0;
			int Qty_rs = 0;
			String Lot_rs ="";
			String ID_rs ="";
			String Loc_rs = "";
			String Lottable05_rs = "";
			String Sku_rs = "";
			Statement st = null;
			
			/*
			String sql = "select CCKEY, CCDROPID, Loc, Sku, Descr, UPCCode, Lottable05, Casecnt, DisplaySystemCount, Lot, ID, Qty from CCDET "
					+ "WHERE Status = 0 and CCDROPID = ? and Sku = ? and Loc = ? order by CCKEY, CCDROPID, Loc, Sku, Descr, UPCCode, Lottable05, Casecnt, DisplaySystemCount, Lot DESC, ID DESC";
	
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, dropid);
			pstmt.setString(2, Sku[a]);
			pstmt.setString(3, Loc[a]);
			*/
			
			String sql = "select CCKEY, CCDROPID, Loc, Sku, Descr, UPCCode, Lottable05, Casecnt, DisplaySystemCount, Lot, ID, Qty from CCDET "
					+ "WHERE Status = 0 and CCDROPID = ? and Seq = ? order by CCKEY, CCDROPID, Loc, Sku, Descr, UPCCode, Lottable05, Casecnt, DisplaySystemCount, Lot DESC, ID DESC";
	
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, dropid);
			pstmt.setString(2, Seq[a]);
			
			
			ResultSet rs = pstmt.executeQuery();
			boolean firstrecord = true;
			
			String CCKEY =null;
			while (rs.next() && CountQty_int_re!=0) {
				//CCKEY = rs.getString("CCKEY");
				Loc_rs = rs.getString("Loc");
				Sku_rs = rs.getString("Sku");
				Lottable05_rs = rs.getString("Lottable05");
				Lot_rs = rs.getString("Lot");
				ID_rs = rs.getString("ID");
				Qty_rs = rs.getInt("Qty");
				
				/*********debug*/
				System.out.println("***************Round " + a);
				System.out.println("Seq: " + Seq);
				System.out.println("Lot_rs: " + Lot_rs);
				System.out.println("ID_rs: " + ID_rs);
				System.out.println("Qty_rs: " + Qty_rs);
				/**********/
				
				if (CountQty_int_re <= Qty_rs) {
					
					/*********debug*/
					System.out.println("CountQty_int_cur: " + CountQty_int_cur);
					System.out.println("CountQty_int_re: " + CountQty_int_re);
					/**********/
					CountQty_int_cur = CountQty_int_re;
					CountQty_int_re = 0;
				}

				if(CountQty_int_re > Qty_rs) {
					CountQty_int_cur = Qty_rs;
					CountQty_int_re = CountQty_int_re - Qty_rs;
				}
				
				String sql2 = "update CCDET set CountQty= ? where CCDROPID = ? and Seq= ? and Sku= ? and Loc = ? and Lot = ? and ID = ? and Lottable05 = ?";
				PreparedStatement pstmt2 = conn.prepareStatement(sql2);
				
				pstmt2.setString(1, dropid);
				pstmt2.setInt(2, CountQty_int_cur);
				pstmt2.setString(3, Seq[a]);
				pstmt2.setString(4, Sku_rs);
				pstmt2.setString(5, Loc_rs);
				pstmt2.setString(6, Lot_rs);
				pstmt2.setString(7, ID_rs);
				pstmt2.setString(8, Lottable05_rs);
				
				pstmt2.executeUpdate();
				
				/*********debug*/	
				System.out.println("CountQty_int_cur: " + CountQty_int_cur);
				System.out.println("CountQty_int_re: " + CountQty_int_re);
				/**********/
				
			}
			
			//for the final row	
			String sql2 = "update CCDET set CountQty= ? where CCDROPID = ? and Seq= ? and Sku= ? and Loc = ? and Lot = ? and ID = ? and Lottable05 = ?";
			PreparedStatement pstmt2 = conn.prepareStatement(sql2);
			
			pstmt2.setString(1, dropid);
			pstmt2.setInt(2, CountQty_int_cur + CountQty_int_re);
			pstmt2.setString(3, Seq[a]);
			pstmt2.setString(4, Sku_rs);
			pstmt2.setString(5, Loc_rs);
			pstmt2.setString(6, Lot_rs);
			pstmt2.setString(7, ID_rs);
			pstmt2.setString(8, Lottable05_rs);
			
			pstmt2.executeUpdate();
			
			
			st = conn.createStatement();
			st.executeUpdate("update CCDET set Status = 2 where Seq=" + Seq[a]);
				
			Statement st3 = conn.createStatement();			
			st3.executeUpdate("update CCHDR set Status = 1 where CCKEY ='" + CCKEY + "'");


			Statement st2 = conn.createStatement();			
			st2.executeUpdate("update CCHDR set Status = 2 where CCKEY ='" + CCKEY + "' and NOT EXISTS (select * from CCDET where CCKEY = '" + CCKEY + "' and Status <> '2' )");

			}
	}
	
	conn.close();
%>